<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="校区" prop="campus">
        <el-select v-model="queryParams.campus" placeholder="请选择校区" clearable>
          <el-option
            v-for="dict in dict.type.campus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.stu_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="英文名" prop="englishName">
        <el-input
          v-model="queryParams.englishName"
          placeholder="请输入学生英文名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级1" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入学生班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['student:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['student:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['student:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['student:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="学生姓名" align="center" prop="name" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="校区" align="center" prop="campus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.campus" :value="scope.row.campus"/>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stu_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="学生英文名" align="center" prop="englishName" />
      <el-table-column label="所属公立校" align="center" prop="publicSchool" />
      <el-table-column label="学生年级" align="center" prop="publicSchoolYear" />
      <el-table-column label="公立校班级" align="center" prop="publicSchoolClass" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['student:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['student:info:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-insert"
            @click="transClass(scope.row)"
            v-hasPermi="['student:info:remove']"
          >转班</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="校区" prop="campus">
          <el-select v-model="form.campus" placeholder="请选择校区">
            <el-option
              v-for="dict in dict.type.campus"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio
              v-for="dict in dict.type.stu_sex"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="学生英文名" prop="englishName">
          <el-input v-model="form.englishName" placeholder="请输入学生英文名" />
        </el-form-item>
        <el-form-item label="所属公立校" prop="publicSchool">
          <el-input v-model="form.publicSchool" placeholder="请输入所属公立校" />
        </el-form-item>
        <el-form-item label="学生年级" prop="publicSchoolYear">
          <el-input v-model="form.publicSchoolYear" placeholder="请输入学生年级" />
        </el-form-item>
        <el-form-item label="公立校班级" prop="publicSchoolClass">
          <el-input v-model="form.publicSchoolClass" placeholder="请输入公立校班级" />
        </el-form-item>
        <el-form-item label="生日" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="民族" prop="ethnicity">
          <el-select v-model="form.ethnicity" placeholder="请选择民族">
            <el-option
              v-for="dict in dict.type.stu_ethnicity"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="身份证" prop="idCardNumber">
          <el-input v-model="form.idCardNumber" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

           <!-- 转班对话 -->
           <el-dialog :title="'转班'" :visible.sync="transDialogVisible" width="600px" append-to-body>
      <el-table
        :data="classesList"
        ref="classTable"
        @selection-change="handleStudentSelectionChange"
        style="height: 400px; overflow-y: auto;"
      >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="课程" align="center" prop="courseName" />
      <el-table-column label="教室" align="center" prop="classroom" />
      <el-table-column label="校区" align="center" prop="campus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.campus" :value="scope.row.campus"/>
        </template>
      </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="transDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitTrans">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/student/info";
import { classList, transClass2 } from "@/api/classes/classes"

export default {
  name: "Info",
  dicts: ['campus', 'stu_ethnicity', 'stu_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      transDialogVisible: false,
      classesList: [],
      classChange:[],
      currentStudent: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        campus: null,
        gender: null,
        englishName: null,
        publicSchool: null,
        publicSchoolYear: null,
        publicSchoolClass: null,
        className: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        campus: [
          { required: true, message: "校区不能为空", trigger: "change" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        englishName: [
          { required: true, message: "学生英文名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        console.log(this.infoList)
        this.total = response.total;
        this.loading = false;
      });
    },
    getClassList(){
      this.loading = true;
      classList().then(response => {
        this.classesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        studentId: null,
        name: null,
        phone: null,
        campus: null,
        gender: null,
        englishName: null,
        publicSchool: null,
        publicSchoolYear: null,
        publicSchoolClass: null,
        birthDate: null,
        ethnicity: null,
        homeAddress: null,
        idCardNumber: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.studentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const studentId = row.studentId || this.ids
      getInfo(studentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.studentId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const studentIds = row.studentId || this.ids;
      this.$modal.confirm('是否确认删除学生信息编号为"' + studentIds + '"的数据项？').then(function() {
        return delInfo(studentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /**转班 */
    transClass(row){
      this.transDialogVisible = true
      this.getClassList();
      this.currentStudent = row;
    },
    handleStudentSelectionChange(selection){
      this.classChange = selection
    },
    submitTrans(){

      const studentId = this.currentStudent.studentId;
      console.log(studentId)
      if(this.classChange.length>1){
        this.$message({
                        message: '只能选择一个班级',
                        type: 'warning'
                    });
                    return
      }
      const classInfo = this.classChange[0]
      console.log(classInfo)
      const data = {
        studentId: studentId,
        classes: classInfo
      };
      transClass2(data).then()
      this.transDialogVisible = false;

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('student/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
